package cn.wowjoy.pda.android_pharmacy.ui.activity.staticH.check;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.NetWorkUtils;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityStaticCheckIndexBinding;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.staticH.StaticCheckIndexPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.staticH.StaticCheckIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.HandInputActivity;

public class StaticCheckIndexActivity extends BaseActivity<ActivityStaticCheckIndexBinding ,StaticCheckIndexViewModel,StaticCheckIndexPresenter> implements StaticCheckIndexPresenter.OnReceivePendingNum {

    //PDA扫码广播
    private ScanReceiver mScanReceiver;
    private IntentFilter intentFilter;

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setPresenter(presenter);
        binding.setViewModel(viewModel);
        binding.hospitalIndexTitle.titleTextTv.setText("药品审核");
        binding.hospitalIndexTitle.titleBackTv.setText("");
        binding.hospitalIndexTitle.titleBackLl.setVisibility(View.VISIBLE);
        binding.hospitalIndexTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        presenter.registerOnReceivePendingNum(this);
        binding.hospitalIndexTitle.titleMenuConfirm.setVisibility(View.VISIBLE);
        binding.hospitalIndexTitle.titleMenuConfirm.setText("手动输入");
        binding.hospitalIndexTitle.titleMenuConfirm.setTextColor(getResources().getColor(R.color.white));
        binding.hospitalIndexTitle.titleMenuConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(StaticCheckIndexActivity.this, HandInputActivity.class);
                startActivityForResult(intent,Contants.REQUEST_CODE);
            }
        });
        //显示操作人姓名
        if(null != PreferenceManager.getInstance().getLUserName() || "".equals(PreferenceManager.getInstance().getLUserName())){
            binding.tvPharmacyCname.setText("工号: "+ PreferenceManager.getInstance().getLUserNum());
        }
        mScanReceiver = new ScanReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(Contants.WOW_NEW_ADD);
        //TODO:界面设置待审核流转单的数量
//        binding.imgvHospitalIndexSaoma.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                presenter.loadData("1833399");
//            }
//        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                switch (resultCode){
                    case Contants.RESULT_HAND_SUCCESS_CODE:
                        //TODO:用户未操作完全的药单 按BACK键返回  刷新界面数据     暂时不考虑
                        String  handInput = data.getExtras().getString("handInput");
                        presenter.quey("99",handInput);
                        break;
                    case Contants.RESULT_HAND_FAIL_CODE:

                        break;
                }
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mScanReceiver, intentFilter);
        presenter.setPendingNum("3");
    }
    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(mScanReceiver);
    }
    @Override
    public void onPending(String onPendingNum) {
        binding.tvHospitalIndexPendingNumber.setText(onPendingNum);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_static_check_index;
    }

    @Override
    protected Class<StaticCheckIndexPresenter> getPresenterClass() {
        return StaticCheckIndexPresenter.class;
    }

    @Override
    protected Class<StaticCheckIndexViewModel> getViewModelClass() {
        return StaticCheckIndexViewModel.class;
    }
    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Contants.WOW_NEW_ADD)) {
                Log.e("TAG", "onReceive: "+intent.getStringExtra(Contants.WOW_NEW_KEY) );
                //要做下扫码的过滤判断
                String stringExtra = intent.getStringExtra(Contants.WOW_NEW_KEY);
//                presenter.loadData(stringExtra.substring(2,stringExtra.length()));
                if (NetWorkUtils.isWifi(StaticCheckIndexActivity.this)) {
                    presenter.quey("99",stringExtra.substring(2,stringExtra.length()));
                } else {
                    CreateDialog.failDialog3(StaticCheckIndexActivity.this, "错误", "当前网络不可用，请检查你的网络设置");
                }
            }
        }
    }
}
